--
-- Funtion: 字符串工具函数
-- Date: 12-9-29
-- Time: 下午5:59
--
module(...,package.seeall)
local math = require "math"
local string = require "string"

--将时间间隔格式化为 hh:mm:ss 格式
function formatDuration(duration)
	local hours = math.floor(duration /3600)
	local minutes = math.floor((duration % 3600)/60)
	local seconds = duration % 60
	local result = string.format("%02d:%02d:%02d",hours,minutes,seconds)
	return result
end

--修正图片名字
function fixImageName(imageName)
	local i, j = string.find(imageName, ".jpg")
	local ii, jj = string.find(imageName, ".png")
	local iii,jjj = string.find(imageName, ".swf")
	local pos = 0
	if j~=nil then
		pos = j
	elseif jj~=nil then
		pos = jj
	elseif jjj ~= nil then
		pos = jjj
	else
		return ""
	end
	local rs = string.sub(imageName, 1, pos)
	local text = string.gsub(rs, ".+%.", "")
	if text == "swf" then
		local k, m = string.find(rs, ".swf")
		if k ~= nil then
			rs = string.sub(rs, 1, k-1) .. ".png"
		end
	end
	return rs
end


function getInterceptString(targetString,interceptString,flag)  --截取string
	local pos=string.find(targetString,interceptString)
	if pos then
		local position=1
		if flag then
			position=0
		end
		local resultString=string.sub(targetString,0,pos-position)
		return resultString,pos
	else
		return targetString
	end
end


function trim(str)
	if str==nil then
		return ""
	end
	str = string.gsub(str, " ", "")
	return str
end

function split(targetString, interceptString)
	-- cclog("interceptString="..interceptString..", targetString="..targetString)
	local resultTable={}
	local pos=string.find(targetString,interceptString)
	while pos do
		resultTable[#resultTable+1]=string.sub(targetString,1,pos-1)
		targetString=string.sub(targetString,pos+1,-1)
		pos=string.find(targetString,interceptString)
	end
	resultTable[#resultTable+1]=targetString
	return resultTable
end

function separateStirng(targetString,interceptString)
	return split(targetString,interceptString)
end

--移除html标签
function removeHtmlTag(str)
	local tag = ">"
	local result = ""
	local length = string.len(str)
	for i=1,length do
		local a = string.sub(str, i, i)
		if a=="<" or a==">" then tag=a end
		if tag==">" and a~=">" then result = result..a end
	end
	return result
end

function getCharFromString(targetString)
	local charTable = {}
	local length = string.len(targetString)
	for i = 1,length do
		charTable[i] = string.sub(targetString,i,i)
	end
	return charTable
end

function jsonToTable(value)
	local jsonString=string.gsub(value,'\"([^\"]-)\":','%1=')
	local jsonString='t='..jsonString
	assert(loadstring(jsonString),"no json"..jsonString)()
	return t
end